package de.medizin.uni.halle.irm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


import com.actionbarsherlock.app.SherlockFragment;

public class ImpulseSettingsFragment extends SherlockFragment {
	private SQLiteDatabase database;
	String i_id = null;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.impulsesettings, container, false);
		view.findViewById(R.id.delete_impulse).setVisibility(View.GONE);
		view.findViewById(R.id.new_impulse).setVisibility(View.GONE);
		updateImpulseList(view);
		
    	((Button) view.findViewById(R.id.save_impulse)).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
				ContentValues impulse = new ContentValues();
				impulse.put("number",((EditText)view.findViewById(R.id.impulse_number)).getText().toString());
				impulse.put("interval",((EditText)view.findViewById(R.id.impulse_interval)).getText().toString());
				database.insert("impulses", null, impulse);
				updateImpulseList(view);
				((EditText) view.findViewById(R.id.impulse_number)).setText("");
	    		((EditText) view.findViewById(R.id.impulse_interval)).setText("");
	    		database.close();
			}
		});   
    	
    	((Button) view.findViewById(R.id.delete_impulse)).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
				database.execSQL("DELETE FROM impulses WHERE _id =" + i_id);
				updateImpulseList(view);
	    		((EditText) view.findViewById(R.id.impulse_number)).setText("");
	    		((EditText) view.findViewById(R.id.impulse_interval)).setText("");			
	    		view.findViewById(R.id.delete_impulse).setVisibility(View.GONE);
	    		view.findViewById(R.id.new_impulse).setVisibility(View.GONE);
	    		view.findViewById(R.id.save_impulse).setVisibility(View.VISIBLE);
			}
		});    	
    	
    	((Button) view.findViewById(R.id.new_impulse)).setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
	    		((EditText) view.findViewById(R.id.impulse_number)).setText("");
	    		((EditText) view.findViewById(R.id.impulse_interval)).setText("");
	    		view.findViewById(R.id.delete_impulse).setVisibility(View.GONE);
	    		view.findViewById(R.id.new_impulse).setVisibility(View.GONE);
	    		view.findViewById(R.id.save_impulse).setVisibility(View.VISIBLE);
			}
		});    
    	
		return view;        
	}
	
	private void updateImpulseList(final View view) {
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
    	ListView lv  = (ListView) view.findViewById(R.id.impulse_list);
    	int[] to = new int[] { R.id.list_impulse_id, R.id.list_impulse_number, R.id.list_impulse_interval };
        String[] fields = new String[] {"_id", "number", "interval"};
    	String q = "select * from impulses order by number;";
    	final Cursor c = database.rawQuery(q, null);
    	CursorAdapter data = new SimpleCursorAdapter(getSherlockActivity(), R.layout.impulse_list, c, fields, to, 0 );
    	lv.setAdapter(data);  
	    lv.setOnItemClickListener(new OnItemClickListener()
	    {
	    	public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
	    	{	
	    		c.moveToPosition(position);
	    		((EditText) view.findViewById(R.id.impulse_number)).setText(c.getString(c.getColumnIndex("number")));
	    		((EditText) view.findViewById(R.id.impulse_interval)).setText(c.getString(c.getColumnIndex("interval")));
	    		i_id = c.getString(c.getColumnIndex("_id"));
	    		view.findViewById(R.id.delete_impulse).setVisibility(View.VISIBLE);
	    		view.findViewById(R.id.new_impulse).setVisibility(View.VISIBLE);
	    		view.findViewById(R.id.save_impulse).setVisibility(View.GONE);
	        }
	     });
	    database.close();
    }
}
